package ihm;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;

import java.awt.GridLayout;
import java.awt.Rectangle;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;

import kernel.Chariot;
import kernel.Noeud;
import kernel.Rail;
import kernel.Simulation;
import kernel.TapisRoulant;
import kernel.Toboggan;

import vue.VueSimulation;

public class SGBag_NewBagageDialog extends JDialog {
    private JLabel labelNoeudArrive = new JLabel();
    private JLabel labelNoeudDepart = new JLabel();
    private JEditorPane editorPaneNoeudArrive = new JEditorPane();
    private JEditorPane editorPaneNoeudDepart = new JEditorPane();
    private JPanel panelBagageSetting = new JPanel();
    private JButton buttonSelectNoeudDepart = new JButton();
    private JButton buttonSelectNoeudArrive = new JButton();
    private JPanel panelNoeudDepart = new JPanel();
    private JPanel panelNoeudArrive = new JPanel();
    private JPanel panelButton = new JPanel();
    private JButton buttonOK = new JButton();
    private JLabel labelSelectBagage = new JLabel();
    private JPanel panelText = new JPanel();
    private SGBag sGBag = null;
    private TapisRoulant noeudDepart = null;
    private Toboggan noeudArrive = null;
    private boolean selectNoeudMode = false;
    private boolean selectNoeudDepart = true;
    private JButton buttonCancelSelect = new JButton();

    public SGBag_NewBagageDialog() {
        this(null, "", false);
    }

    public SGBag_NewBagageDialog(Frame parent, String title, boolean modal) {
        super(parent, title, modal);
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setSize(new Dimension(235, 165));
        this.getContentPane().setLayout(new BorderLayout());
        this.setBounds(new Rectangle(10, 10, 230, 160));
        this.setFont(new Font("Cambria", 0, 12));
        labelNoeudArrive.setText("Arrive");
        labelNoeudArrive.setBounds(new Rectangle(65, 110, 42, 15));
        labelNoeudDepart.setText("Depart");
        labelNoeudDepart.setBounds(new Rectangle(65, 70, 42, 15));
        editorPaneNoeudDepart.setEditable(false);
        editorPaneNoeudArrive.setEditable(false);
        panelBagageSetting.setBounds(new Rectangle(0, 0, 10, 10));
        panelBagageSetting.setLayout(new BorderLayout());
        panelBagageSetting.setSize(new Dimension(293, 68));
        buttonSelectNoeudDepart.setText("...");
        buttonSelectNoeudDepart.setFont(new Font("YaHei Consolas Hybrid", 0, 12));
        buttonSelectNoeudDepart.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    buttonSelectNoeudDepart_mouseClicked(e);
                }
            });
        buttonSelectNoeudArrive.setText("...");
        buttonSelectNoeudArrive.setFont(new Font("YaHei Consolas Hybrid", 0, 12));
        buttonSelectNoeudArrive.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    buttonSelectNoeudArrive_mouseClicked(e);
                }
            });
        panelButton.setSize(new Dimension(293, 32));
        buttonOK.setText("Ok");
        buttonOK.setFont(new Font("Cambria", 0, 12));
        buttonOK.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    buttonOK_mouseClicked(e);
                }
            });
        labelSelectBagage.setText("Please select a Depart and a Arrive");
        labelSelectBagage.setFont(new Font("Calibri", 0, 12));
        buttonCancelSelect.setText("Cancel Select");
        buttonCancelSelect.setFont(new Font("Cambria", 0, 12));
        buttonCancelSelect.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    buttonCancelSelect_mouseClicked(e);
                }
            });
        this.getContentPane().add(panelBagageSetting, BorderLayout.NORTH);
        panelButton.add(buttonOK, null);
        panelButton.add(buttonCancelSelect, null);
        this.getContentPane().add(panelButton, BorderLayout.CENTER);
        panelNoeudDepart.add(labelNoeudDepart, null);
        panelNoeudDepart.add(editorPaneNoeudDepart, null);
        panelNoeudDepart.add(buttonSelectNoeudDepart, null);
        panelNoeudArrive.add(labelNoeudArrive, null);
        panelNoeudArrive.add(editorPaneNoeudArrive, null);
        panelNoeudArrive.add(buttonSelectNoeudArrive, null);
        panelBagageSetting.add(panelNoeudDepart, BorderLayout.CENTER);
        panelBagageSetting.add(panelNoeudArrive, BorderLayout.SOUTH);
        panelText.add(labelSelectBagage, null);
        panelBagageSetting.add(panelText, BorderLayout.NORTH);
    }

    private void buttonOK_mouseClicked(MouseEvent e) {
        /*
         * creer le bagage avec le depart et l'arrive choisi
         * remettre l'ecran a l'etat initial
         */
        
        // si le depart et l'arrive sont non null on creer le babage
        if ((noeudDepart != null) && (noeudArrive != null)){
            getSGBag().getSimulation().creerBagage(noeudDepart, noeudArrive);
            Rail rail =(Rail)(noeudDepart.getListArcDep().firstElement());
            Chariot chariot = rail.getListChariot().lastElement();
            getSGBag().getPanelVueSimulation().addVueChariot(chariot, 15, 15);
        }
        
        // reinitialiser
        buttonCancelSelect_mouseClicked(null);
        setVisible(false);
        noeudDepart = null;
        noeudArrive = null;
        editorPaneNoeudDepart.setText("");
        editorPaneNoeudArrive.setText("");
        selectNoeudMode = false;
        selectNoeudDepart = true;
        labelSelectBagage.setText("Choisissez un tapis Roulant et un Toboggan");
    }

    public void setSGBag(SGBag sGBag) {
        this.sGBag = sGBag;
    }

    public SGBag getSGBag() {
        return sGBag;
    }

    private void buttonSelectNoeudDepart_mouseClicked(MouseEvent e) {
        // choisir le noeud de depart
        selectNoeudDepart = true;
        labelSelectBagage.setText("Please click on a TapisRoulant");
        buttonSelectNoeud_mouseClickedEvent();
    }

    private void buttonSelectNoeudArrive_mouseClicked(MouseEvent e) {
        // choisir le noeud d'arrive
        selectNoeudDepart = false;
        labelSelectBagage.setText("Please click on a Toboggan");
        buttonSelectNoeud_mouseClickedEvent();
    }
    
    private void buttonSelectNoeud_mouseClickedEvent(){
        // changer le type de cursor lorsqu'on est dans le mode de selection
        selectNoeudMode = true;
        sGBag.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }
    
    public boolean isSelectNoeudMode() {
        return selectNoeudMode;
    }

    void selectNoeud(Noeud noeud) {
        /*
         * reconnaitre le noeud donner par l'untilisateur et de la mise en place
         */
        if (noeud == null){
            
        }
        else{
            if(selectNoeudDepart & (noeud instanceof TapisRoulant)){
                // si on a voulu selectionner un noeud de depart
                noeudDepart = (TapisRoulant) noeud;
                editorPaneNoeudDepart.setText("TapisRoulant : " + noeudDepart.getId());
                selectNoeudMode = false;
            }
            else if(!selectNoeudDepart & (noeud instanceof Toboggan)){
                // si on a voulu selectionner un noeud d'arrive
                noeudArrive = (Toboggan) noeud;
                editorPaneNoeudArrive.setText("Toboggan : " + noeudArrive.getId());
                selectNoeudMode = false;
            }
        }
        
        // reinitialiser apres avoir fini l'initialisation
        if (!selectNoeudMode){
            sGBag.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
        }
        if ((noeudDepart!=null)&(noeudArrive!=null)){
            labelSelectBagage.setText("Please click Ok to create bagage");
        }
    }

    private void buttonCancelSelect_mouseClicked(MouseEvent e) {
        // sortir l'etat de selection
        selectNoeudMode = false;
        sGBag.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
}
